import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class ValidCourse extends HttpServlet{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
	response.setContentType("text/html");
	PrintWriter pw = response.getWriter();
	String courseCode = request.getParameter("courseCode");
	

	//connect to the database
	Connection con = null;
	Statement stmt = null;
	ResultSet rs = null;
	String query = "SELECT code from course";
	try {
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		con = DriverManager.getConnection("jdbc:mysql://bungle08.cs.toronto.edu:3306/group22",
			"group22", "123456");
	
		if(!con.isClosed()){
			stmt = con.createStatement();		
			rs = stmt.executeQuery(query);		
			while (rs.next()) {
	
				if(courseCode.equals(rs.getString("code"))){
					pw.println("this course is offered this semester");
				}else{
					pw.println("this course will not be offered this semester");
				}
			}
			//non existing user
			pw.println("not a valid user");
			
		}
	}
	 catch(Exception e) {
		pw.println("Exception: " + e.getMessage());
	} finally {
		try {
			if(con != null)
				con.close();
		} catch(SQLException e) {}
	}}
}
		